﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using ChatServerApp.Classes;

namespace ChatServerApp.Tools
{
    class ActiveComponents
    {
        private static List<User_Complete> userList = new List<User_Complete>();

        public static List<User_Complete> UserList
        {
            get { return userList; }
        }

        public static void UserAdd(User_Complete user)
        {
            ActiveComponents.userList.Add(user);
            writeXML();
        }

        public static void FillUserList()
        {
            if (File.Exists(Tools.USER_DATA))
                readXML();
        }

        private static void writeXML()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<User_Complete>));
            FileStream fs = new FileStream(Tools.USER_DATA, FileMode.Create);
            ser.Serialize(fs, userList);
            fs.Close();
        }

        private static void readXML()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<User_Complete>));
            StreamReader sr = new StreamReader(Tools.USER_DATA);
            userList = (List<User_Complete>)ser.Deserialize(sr);
            sr.Close();
        }

        public static void Update()
        {
            writeXML();
        }
    }
}
